<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="js/script.js"></script>
    <link rel="stylesheet" href="css/nav_bar.css">
    <link rel="stylesheet" href="css/add_questions.css">
    <title>录题</title>
</head>
<body>
    <div class="nav_bar">
        <a href="/admis.html">首页</a>
        <a href="/add_questions.html" class="choosed">录题</a>
        <a href="/sign in.html" class="last_li">登录/注册</a>
    </div>
    <div class="midden">
        <div class="block_line"></div>
        <div class="head_line">录题</div>
        <div class="box">
            <table class="box1">
                <tr class="in">
                    <td class="first">题目名称</td>
                    <td class="second"><input id="title"></input></td>
                </tr>
                <tr class="in">
                    <td class="first">难度</td>
                    <td class="second"><input id="level"></input></td>
                </tr>
                <tr class="area">
                    <td class="first">描述</td>
                    <td class="second"><textarea id="desc"></textarea>
                </tr>
                <tr class="area">
                    <td class="first">头文件</td>
                    <td class="second"><textarea id="header"></textarea>
                </tr>
                <tr class="area">
                    <td class="first">用户初始代码</td>
                    <td class="second"><textarea id="body"></textarea>
                </tr>
                <tr class="area">
                    <td class="first">测试代码</td>
                    <td class="second"><textarea id="tail"></textarea>
                </tr>
                <tr class="in">
                    <td class="first">时间限制(单位:s)</td>
                    <td class="second"><input id="cpu"></input>
                </tr>
                <tr class="in">
                    <td class="first">内存大小限制(单位:kb)</td>
                    <td class="second"><input id="mem"></input></td>
                </tr>
            </table>
            <div class="box2">
                <button class="btn" onclick="submit()">添加</button>
            </div>
        </div>
    </div>
    <script>
        CheckAdministor();

        function submit() {
            // 获取信息
            let title = $("#title").val();
            if (title == '') {
                alert('题目名称不能为空');
                return;
            }
            let level = $("#level").val();
            if (level == '') {
                alert('题目难度不能为空');
                return;
            }
            let desc = $("#desc").val();
            if (desc == '') {
                alert('题目描述不能为空');
                return;
            }
            let header = $("#header").val();
            if (header == '') {
                alert('头文件部分不能为空');
                return;
            }
            let body = $("#body").val();
            if (body == '') {
                alert('用户代码不能为空');
                return;
            }
            let tail = $("#tail").val();
            if (tail == '') {
                alert('测试代码不能为空');
                return;
            }
            let cpu = $("#cpu").val();
            if (cpu == '') {
                alert('时间限制不能为空');
                return;
            }
            let mem = $("#mem").val();
            if (mem == '') {
                alert('内存限制不能为空');
                return;
            }

            $.ajax({
                method: 'Post',
                url: '/add_question',
                dataType: 'json',
                contentType: 'application/josn;charset=utf-8',

                data: JSON.stringify({
                    'title': title,
                    'level': level,
                    'desc': desc,
                    'header': header,
                    'body': body,
                    'tail': tail,
                    'cpu': cpu,
                    'mem': mem
                }),

                success: function (data) {
                    console.log(data);
                    if (data.result == "success") {
                        alert('添加成功');
                        location.href = "/add_questions.html";
                        window.event.returnValue = false;
                    }
                    else {
                        alert('添加失败');
                    }
                }
            })
        }
    </script>
</body>
</html>